package com.cloudlinkscm.base.filecenter.service.impl;

import com.cloudlinkscm.base.filecenter.assemblers.DocumentUploadDto2DocumentSaveBoAssembler;
import com.cloudlinkscm.base.filecenter.dao.FileRecordDao;
import com.cloudlinkscm.base.filecenter.dao.FileStreamDao;
import com.cloudlinkscm.base.filecenter.domain.FileStorageFactory;
import com.cloudlinkscm.base.filecenter.exception.FileStreamNotExistException;
import com.cloudlinkscm.base.filecenter.model.FileRecord;
import com.cloudlinkscm.base.filecenter.model.FileStream;
import com.cloudlinkscm.base.filecenter.model.bo.DownloadResponseBo;
import com.cloudlinkscm.base.filecenter.model.dto.DeleteRequestDto;
import com.cloudlinkscm.base.filecenter.model.dto.DocumentUploadDto;
import com.cloudlinkscm.base.filecenter.model.dto.ImageUploadDto;
import com.cloudlinkscm.base.filecenter.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by syq on 2017/7/22.
 */
@Service
@Transactional
public class FileServiceImpl implements FileService {
    @Autowired
    private FileRecordDao fileRecordDao;
    @Autowired
    private FileStreamDao fileStreamDao;
    @Autowired
    private DocumentUploadDto2DocumentSaveBoAssembler documentUploadDto2DocumentSaveBoAssembler;

    @Override
    public Map<String, String> documentStreamIds(Collection<String> fileIds) {
        Map<String, String> map = new HashMap<>();
        for (String fileId : fileIds) {
            FileRecord fileRecord = fileRecordDao.selectByPrimaryKey(fileId);
            map.put(fileId, FileStorageFactory.create(fileRecord.getStorageType()).createStream(fileRecord).getId());
        }
        return map;
    }

    @Override
    public Map<String, String> downloadImages(Collection<String> fileIds) {
        throw new NotImplementedException();
    }

    @Override
    public String uploadDocument(MultipartFile file, DocumentUploadDto request) {
        return FileStorageFactory.create(request.getStoreType()).saveDocument(documentUploadDto2DocumentSaveBoAssembler.assemble(request), file).getId();
    }

    @Override
    public String uploadImage(MultipartFile file, ImageUploadDto request) {
        //TODO: 暂时采用文档上传处理
        throw new NotImplementedException();
    }

    @Override
    public int delete(DeleteRequestDto dto) {
        for (String fileId : dto.getFileIds()) {
            FileRecord record = fileRecordDao.selectByPrimaryKey(fileId);
            if (!record.getSysCode().equals(dto.getSysCode())) {
                break;
            }
            if (record.getDelete()) {
                break;
            }

            return FileStorageFactory.create(record.getStorageType()).delete(record);
        }
        return 0;
    }

    @Override
    public DownloadResponseBo download(String streamId) {
        FileStream stream = fileStreamDao.selectByPrimaryKey(streamId);
        if (stream == null) {
            throw new FileStreamNotExistException();
        }
        InputStream inputStream = FileStorageFactory.create(stream.getStorageType()).getFileInputStream(stream);

        return new DownloadResponseBo(inputStream, stream);
    }

}

